package com.mengdie.shuidi.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.model.BaseModel;
import com.mengdie.shuidi.model.DiscountEntity;
import com.mengdie.shuidi.ui.adapter.MyDiscountAdapter;
import com.mengdie.shuidi.utils.ToastUtil;
import com.mengdie.shuidi.utils.core.EmptyUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;


/**
 * 作者：HQY on 2019/4/11 14:56
 * 邮箱：hqy_xz@126.com
 */
public class MyDiscountActivity extends BaseActivity {

    @Bind(R.id.ll_discount_null)
    LinearLayout mLlDiscountNull;
    @Bind(R.id.rv_discount_list)
    RecyclerView mRvDiscountList;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;


    private List<DiscountEntity> mDiscountList = new ArrayList<>();
    private MyDiscountAdapter mDiscountAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_my_discount);
        ButterKnife.bind(this);
        initView();

    }


    private void initView() {
        mTvGenericTitle.setText("我的优惠券");
        mDiscountAdapter = new MyDiscountAdapter(mDiscountList);
        mRvDiscountList.setLayoutManager(new LinearLayoutManager(MyDiscountActivity.this));
        mRvDiscountList.setAdapter(mDiscountAdapter);
        getCouponList();

    }

    private void getCouponList() {
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(MyDiscountActivity.this))
                .post(AppContext.getInstance().getBaseUrl() + "ucenter/coupon_list")
                .tag(MyDiscountActivity.this)
                .upJson(mapSession())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        Type discountType = new TypeToken<BaseModel<List<DiscountEntity>>>() {
                        }.getType();
                        BaseModel<List<DiscountEntity>> disCountList;
                        try {
                            disCountList = gson.fromJson(s, discountType);
                        } catch (JsonSyntaxException e) {
                            disCountList = gson.fromJson(s, BaseModel.class);
                        }
                        if (disCountList.getRet().equals("0")) {

                            mDiscountList = disCountList.getData();
                            if (EmptyUtils.isNotEmpty(mDiscountList)) {
                                mLlDiscountNull.setVisibility(View.GONE);
                                mDiscountAdapter.setNewData(mDiscountList);
                            } else {
                                mLlDiscountNull.setVisibility(View.VISIBLE);
                            }

                        } else {
                            ToastUtil.showToast(disCountList.getMsg());
                            mLlDiscountNull.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }


    @OnClick(R.id.rl_generic_back)
    public void onViewClicked() {
        finish();
    }
}
